package com.lingyun.service.impl;

import com.lingyun.model.db.SatelliteRsData;
import com.lingyun.dao.SatelliteRsDataRepository;
import com.lingyun.service.SatelliteRsDataService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import java.util.ArrayList;
import java.util.List;


/**
 * 数据库表【satellite_rs_data】对应的service实现类
 *
 * @author bjs
 * @date 2017-07-25 00:15:22
 */
@Service
public class SatelliteRsDataServiceImpl implements SatelliteRsDataService {

    private static final Log logger = LogFactory.getLog(SatelliteRsDataServiceImpl.class);

    @Autowired
    private SatelliteRsDataRepository satelliteRsDataRepository;


    @PersistenceContext
    private EntityManager entityManager;


    //===================================================================================================

    /**
     * 保存或更新对象
     *
     * @param satelliteRsData
     */
    @Override
    public void save(SatelliteRsData satelliteRsData) {
        satelliteRsDataRepository.save(satelliteRsData);
    }


    /**
     * 通过id查找对象
     *
     * @param id
     * @return
     */
    @Override
    public SatelliteRsData findById(Long id) {
        return satelliteRsDataRepository.findOne(id);
    }

    @Override
    public List<SatelliteRsData> findByParams(String startDate, String endDate, float minLat, float maxLat, float minLng, float maxLng) {
        StringBuffer sqlForList = new StringBuffer("select c from SatelliteRsData c where 1=1 ");

        sqlForList.append(" AND (c.acquisitionDate >= '" + startDate + "' AND c.acquisitionDate <= '" + endDate + "') ");
        sqlForList.append(" AND ABS(" + minLat + " + " + maxLat + " - c.minLat - c.maxLat) <= (ABS(" + minLat + " - " + maxLat + ")+ABS(c.minLat - c.maxLat)) ");
        sqlForList.append(" AND ABS(" + minLng + " + " + maxLng + " - c.minLng - c.maxLng) <= (ABS(" + minLng + " - " + maxLng + ")+ABS(c.minLng - c.maxLng)) ");
        sqlForList.append(" order by c.id desc ");

        TypedQuery queryForList = entityManager.createQuery(sqlForList.toString(), SatelliteRsData.class);
        List<SatelliteRsData> list = queryForList.getResultList();

        return list;
    }


    /**
     * 提供给管理使用的分页查询
     *
     * @param pageable
     * @return
     */
    @Override
    public Page<SatelliteRsData> findForAdminList(Pageable pageable) {
        StringBuffer sqlForCount = new StringBuffer("select count(c.id) from SatelliteRsData c where 1=1 ");
        StringBuffer sqlForList = new StringBuffer("select c from SatelliteRsData c where 1=1 ");

        /*
        if (StringUtils.isNotBlank(name)) {
            sqlForCount.append(" and ( c.name like  '%" + name + "%' or c.username like '%" + name + "%' )  ");
            sqlForList.append(" and ( c.name like  '%" + name + "%' or c.username like '%" + name + "%' )  ");
        }
        */

        sqlForList.append(" order by c.id desc ");

        TypedQuery queryForCount = entityManager.createQuery(sqlForCount.toString(), Long.class);
        Long count = (Long) queryForCount.getSingleResult();

        TypedQuery queryForList = entityManager.createQuery(sqlForList.toString(), SatelliteRsData.class);
        List<SatelliteRsData> adminList = queryForList.setMaxResults(pageable.getPageSize()).setFirstResult(pageable.getOffset()).getResultList();

        Page<SatelliteRsData> pageRst = new PageImpl<SatelliteRsData>(adminList, pageable, count);
        return pageRst;
    }


}
